﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Bll;
using EQT.Model;
using EQT.Dal;
using System.Collections.Generic;
/// <summary>
/// UploadTestItem 的摘要说明
/// </summary>
public class UploadTestItem : IUploadFile
{
    public UploadTestItem()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private DaTestCatalog daCatalog = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTestCatalog") as DaTestCatalog;
    private DaFileTemplate daTemplate = DevFuture.Common.WorkContext.AppCtx.GetObject("DaFileTemplate") as DaFileTemplate;
    private DaTestItem daItem = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTestItem") as DaTestItem;
    #region IUploadFile 成员

    public void InitUpload(IUploadPage up, System.Collections.Specialized.NameValueCollection nvc)
    {
        //给检验项的EID，给定返回URL

        up.SetConfigValue("UPLOAD_MODE", "testitem");
        string templateid = nvc["id"];//获取检验项ID号
        string type = nvc["type"];
        string rtnctgid = nvc["rtn_ctgid"];
        string rtnitemid = nvc["rtn_itemid"];
        string pageindex = nvc["pageindex"];

        if (String.IsNullOrEmpty(templateid) || templateid.Trim().Length == 0 || String.IsNullOrEmpty(type) || type.Trim().Length == 0)
        {
            Utility.SiteError("缺失所必需的参数【检验项编号或模板类型】", up.Response);
            return;
        }
        else
        {
            up.SetConfigValue("TEMPLATE_ID", templateid);
            up.SetConfigValue("TEMPLATE_TYPE", type);
            up.SetConfigValue("RTN_CTG_ID",rtnctgid);
            up.SetConfigValue("RTN_ITEM_ID", rtnitemid);
            up.SetConfigValue("PAGEINDEX", pageindex);
        }
        MoFileTemplate mot = new MoFileTemplate();
        mot.FtId = templateid;

        daTemplate.GetEntityEx(ref mot);
        MoTestCatalog moc = new MoTestCatalog();
        moc.TestCtgId = mot.ItemCode;
        daCatalog.GetEntityEx(ref moc);

        up.SetPageTitle( String.Format("为检验项【{0}】【{1}】上传{2}模板：",
            moc.TestCtgId, moc.TestCtgName.Substring(0, (moc.TestCtgName.Length > 10) ? 10 : moc.TestCtgName.Length) + "...", type == "doc" ? "Word" : "Excel"));
    }

    public int Upload1(IUploadPage up, MoTfiles file, string opter_id)
    {
        MoRefFile modref = new MoRefFile();
        modref.Eid = up.GetConfigValue("TEMPLATE_ID").ToString();
        modref.FileId = file.FileId;
        modref.RfId = SequenceKeys.GenerateReferFileID();
        int rc = 0;
        try
        {
            if (up.GetConfigValue("TEMPLATE_TYPE").ToString() == "doc")
            {
                rc=daItem.AddTemplateDoc(file, modref);
            }
            else
            {
                rc=daItem.AddTemplateXls(file, modref);
            }
        }
        catch (DalException de)
        {
            Utility.GlobalLog.Error("添加单个文件失败!", de);
            Utility.SiteError("添加单个文件失败!", up.Response);
            return -1;
        }
        return rc;
    }

    public int UploadedMuliti(IUploadPage up, List<MoTfiles> files, string opter_id)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public string GetReturnUrl(IUploadPage up)
    {
        return String.Format("TestItem.aspx?rtnctgid={0}&rtnitemid={1}&pageindex={2}", up.GetConfigValue("RTN_CTG_ID"),up.GetConfigValue("RTN_ITEM_ID"), up.GetConfigValue("PAGEINDEX"));
    }

    #endregion
}
